<?php
class Pagination extends Component{
	public function output(){
		$pagination = $this->data['pagination'];
		if(!$pagination instanceof Mpagination){
			throw new Dexception('No pagination Model Matched!');
		}
		$pageTotal = ceil(($pagination->total)/$pagination->pagesize);
		$showItemNum = 5;
		$preshow = ceil($showItemNum/2);
		$nextshow = $showItemNum - $preshow;
		$next = ($pagination->page+1>$pageTotal)?null:$pagination->page+1;
		$pre = ($pagination->page-1<1)?null:$pagination->page-1;
		$html = "<div>";
		$html .= "<style>
		.pagination_item{
			float:left;
			width:20px;
			height:20px;
			border:1px solid #ccc;
			margin-left:5px;
			text-align:center;
			line-height:20px;
			color:black;
		}
		a.pagination_item:hover,.pagination_item_current{
			color:#FA4100;
		}
		</style>";
		$html .= "<a class='pagination_item' style='width:50px;' href='{$pagination[$pre]}'>上一页</a>";
		for($i=1;$i<=$pageTotal;$i++){
			Debug::record('1:i='.$i.'page-pre:'.($pagination->page-$preshow).'page+next'.($pagination->page+$nextshow));
				if($i<=($pagination->page-$preshow)) continue;
				if($i>($pagination->page+$nextshow)) continue;

			if($i==$pagination->page){
				$html .= "<a class='pagination_item pagination_item_current' href='{$pagination[$i]}'>$i</a>";
			}else{
				$html .= "<a class='pagination_item' href='{$pagination[$i]}'>$i</a>";
			}
		}
		$html .= "<a class='pagination_item' style='width:50px;' href='{$pagination[$next]}'>下一页</a>";
		$html .='</div>';
		return $html;
	}
}


